﻿/// <reference path="core/Error.js"/>
/// <reference path="core/FaScript.js"/>
/// <reference path="core/FaScript.Event.js"/>

"use strict";
Namespace('FaScript.Mouse');

FaScript.Mouse.defineClass('MouseOperater',
	function (ID) {
	    var object = Object.create(null);
	    var $w = $(window);

	    //私有变量
	    var _id = ID;
	    var _exec = function (e) {
	        //判断是否满足触发条件
	        if (e.pageX >= _Area.left
                && e.pageX <= _Area.right
                && e.pageY >= _Area.top
                && e.pageY <= _Area.bottom) {
	            object.Event.trigger();
	        }
	    }

	    //属性
	    var _Area = { left: 0, right: 0, top: 0, bottom: 0 },
            _Keys = { ctrl: false, alt: false, shift: false },
            _Buttons = { left: false, right: false, middle: false };
	    Object.defineProperties(object,
            {
                'Area': { get: function () { return _Area; }, set: function (value) { $.extend(_Area, value); } },
                'Keys': { get: function () { return _Keys; }, set: function (value) { $.extend(_Keys, value); } },
                'Buttons': { get: function () { return _Buttons; }, set: function (value) { $.extend(_Buttons, value); } },
                'Event': { value: new FaScript.Event() }
            });

	    //方法
	    Object.defineProperty(object, 'Enable', {
	        value: function () {
	            /// <summary>启用</summary>

	            FaScript.Mouse.MouseOperater.List[_id] = object;

	            var _width = $w.width(),
                    _height = $w.height();
	            $.each(_Area, function (name, value) {
	                value = parseFloat(value);
	                var tmpTotal = name == 'left' || name == 'right' ? _width : _height;

	                if (value > 0 && value < 1) {
	                    _Area[name] = value * _width;
	                } else if (value > -1 && value < 0) {
	                    _Area[name] = _width + value * _width;
	                } else if (value < -1) {
	                    _Area[name] = _width + value;
	                }
	            });
	            _Area.right = _width - _Area.right;
	            _Area.bottom = _height - _Area.bottom;

	            $w.bind('mousemove', _exec);
	        }
	    });
	    Object.defineProperty(object, 'Disable', {
	        value: function () {
	            /// <summary>禁用</summary>

	            $w.unbind('mousemove', _exec);
	        }
	    });

        //锁定成员
	    Object.seal(object);
	    return object;
	});

FaScript.Mouse.MouseOperater.List = {};